package com.ytjj.qmyx.mall.controller;

import com.alibaba.fastjson.JSON;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.ytjj.common.api.CommonResult;
import com.ytjj.common.constants.QueueConstants;
import com.ytjj.common.controller.BaseController;
import com.ytjj.common.utils.TokenUtil;
import com.ytjj.common.service.RedisService;
import com.ytjj.common.utils.HttpClientUtil;
import com.ytjj.common.utils.feishu.FeiShuUtil;
import com.ytjj.qmyx.mall.config.CommonPropertiesConfig;
import com.ytjj.qmyx.mall.config.SupplyApiConfig;
import com.ytjj.qmyx.mall.service.OrdersService;
import com.ytjj.qmyx.mall.service.impl.OrdersServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeoutException;


@Slf4j
@RestController
@RequestMapping("/test")
public class TestController extends BaseController {

    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Resource
    private OrdersServiceImpl ordersServiceImpl;
    @Resource(name = "mallCommonPropertiesConfig")
    private CommonPropertiesConfig commonPropertiesConfig;

    @Autowired
    private SupplyApiConfig supplyApiConfig;
    @Autowired
    private RedisService redisService;

    @GetMapping("/check")
    public String check() {
        return "ok";
    }

    @GetMapping("/sendTest")
    public CommonResult sendTest() {
        log.info("===========sendTest=========");
        rabbitTemplate.convertAndSend(QueueConstants.TEST_QUEUE, "test123");
        return CommonResult.success();
    }

    @GetMapping("/sendFeishu")
    public String sendFeishu() {
        FeiShuUtil.sendMsg("test");
        return "ok";
    }

    @GetMapping("/testPayStatus")
    public String testPayStatus() {

        ordersServiceImpl.checkPaymentChannelStatus("wx");
        return "ok";
    }

    /**
     * 刷新黑名单到redis
     * @return
     */
    @GetMapping("/getBalckUsers")
    public String getH5Address() {
        String resultJson = HttpClientUtil.doGet(supplyApiConfig.getUrl() + "/banker/getBalckUsers", null, supplyApiConfig.getHeadsMap());
        List<String> list = JSON.parseArray(resultJson,String.class);
        if (CollectionUtils.isNotEmpty(list)){
            for (String tel:list){
                redisService.delete("balck:tel:"+tel);
                redisService.set("balck:tel:"+tel,"1");
            }
        }
        return resultJson;
    }

    @GetMapping("/sendResponse")
    public Object sendResponse(String no,String comp) {
        Object o = ordersServiceImpl.queryOrderExpressInfo(comp, no);
        return o;
    }


    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory factory = new ConnectionFactory();
        // 设置接入点，在消息队列RabbitMQ版控制台实例详情页面查看。
//        factory.setHost("amqp-cn-wwo30giw0005.cn-shenzhen.amqp-0.net.mq.amqp.aliyuncs.com");
        factory.setHost("xxxxxx");
        // 用户名，在消息队列RabbitMQ版控制台静态用户名密码页面查看。
//        factory.setUsername("MjphbXFwLWNuLXd3bzMwZ2l3MDAwNTpMVEFJNXRMZ1FIVUdESHBLUnFYaXlxSEI=");
        factory.setUsername("xxxx");
        // 密码，在消息队列RabbitMQ版控制台静态用户名密码页面查看。
//        factory.setPassword("QjdCRTZBNzQ2MTRFNkVBOEQ1RkM4M0IxRUU0RTRGOEM2MTk2MjcyMDoxNjcxNjgwMDE3NDg2");
        factory.setPassword("xxxx");
        //设置为true,开启Connection自动恢复功能；设置为false,关闭Connection自动恢复功能。
        factory.setAutomaticRecoveryEnabled(true);
        factory.setNetworkRecoveryInterval(5000);
        // 设置Vhost名称，请确保已在消息队列RabbitMQ版控制台上创建完成。
        factory.setVirtualHost("guchuangmall");
        // 默认端口，非加密端口5672，加密端口5671。
        factory.setPort(5672);
        // 基于网络环境合理设置超时时间。
        factory.setConnectionTimeout(30 * 1000);
        factory.setHandshakeTimeout(30 * 1000);
        factory.setShutdownTimeout(0);
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
//        channel.exchangeDeclare("amq.fanout", "fanout", true, false, false, null);
        channel.queueDeclare("test.queue", true, false, false, new HashMap<String, Object>());
        channel.basicPublish("","test.queue",null,"test mxl".getBytes());
//        channel.queueBind("test.queue", "amq.direct", "test");
        // 开始发送消息。
//        for (int i = 0; i < 10; i++  ) {
//            // ${ExchangeName}必须在消息队列RabbitMQ版控制台上已存在，并且Exchange的类型与控制台上的类型一致。
//            // BindingKey根据业务需求填入相应的BindingKey。
//            AMQP.BasicProperties props = new AMQP.BasicProperties.Builder().messageId(UUID.randomUUID().toString()).build();
//            channel.basicPublish("amq.direct", "test", true, props,
//                    ("消息发送Body"  + i).getBytes(StandardCharsets.UTF_8));
//        }
        connection.close();
    }

}
